// 云函数入口文件
const cloud = require('wx-server-sdk')
Array.prototype.remove = function (val) {
  var index = this.indexOf(val);
  if (index > -1) {
    this.splice(index, 1);
  }
}
cloud.init()
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let openid = wxContext.OPENID;
  const {
    act
  } = event;
  switch (act) {
    case 'add':
      const {
        fopenid,
        fname
      } = event;
      try {
        let proc = await db.collection('friend').where({
          openid,
          friends: fopenid
        }).count();
        if (proc.total > 0) {
          return {
            code: 2,
            mes: fname + '已经是你的好友'
          }
        } else {
          await db.collection('friend').where({
            openid
          }).update({
            data: {
              friends: _.push(fopenid)
            }
          });
          return {
            code: 0,
            mes: '成功添加' + fname + '为好友'
          }
        }
      } catch (e) {
        return {
          code: 1,
          mes: '添加' + fname + '失败'
        }
      }
      break;
    case 'get':
      let profops = await db.collection('friend').where({
        openid
      }).field({
        friends: true
      }).get();
      let fops = profops.data[0].friends;
      let profl = await db.collection('friend').where({
        openid: _.in(fops)
      }).orderBy('userInfo.nickName', 'asc').field({
        userInfo: true
      }).get();
      return {
        fl: profl.data,
        openid
      }
      break;
    case 'search':
      try {
        const { keyword } = event;
        let kreg = keyword.replace(/\\/g, '\\\\').replace(/\./g, '\\.').replace(/\*/g, '\\*');
        let prosear = await db.collection('friend').where({
          'userInfo.nickName': db.RegExp({
            regexp: kreg,
            options: 'i'
          }),
          openid: _.neq(openid)
        }).field({
          friends: false
        }).orderBy('userInfo.nickName', 'asc').get();

        let sl = prosear.data;
        let profop = await db.collection('friend').where({
          openid
        }).field({ friends: true }).get();
        let friends = profop.data[0].friends;
        fsl = sl.map(v => {
          v.isf = false;
          if (friends.indexOf(v.openid) != -1) {
            v.isf = true
          }
          return v;
        })

        return {
          fsl
        }
      } catch (e) {
        return { fsl: [] }
      }
      break;
    case 'del':
      let fdo = event.fopenid;
      try {
        let profdop = await db.collection('friend').where({
          openid
        }).field({
          friends: true
        }).get();

        let fdop = profdop.data[0].friends;
        fdop.remove(fdo);
        await db.collection('friend').where({
          openid
        }).update({
          data: {
            friends: fdop
          }
        });
        return {
          code: 0,
          delOpenid: fdo,
          mes: '删除成功'
        }
      } catch (e) {
        return {
          code: 1,
          delOpenid: fdo,
          mes: '删除失败'
        }
      }
      break;

    case 'share':
      let fsop = event.fop;
      
      try {
        //将分享者加入被分享者的好友列表
        let prosc = await db.collection('friend').where({
          openid,
          friends: fsop
        }).count();
          
        if (prosc.total == 0){
          await db.collection('friend').where({
            openid
          }).update({
            data: {
              friends: _.push(fsop)
            }
          });

          //将被分享者加入分享者的好友列表
          let prosac= await db.collection('friend').where({
            openid:fsop,
            friends:openid
          }).count();
          if(prosac.total==0){
            await db.collection('friend').where({
              openid:fsop
            }).update({
              data:{
                friends:_.push(openid)
              }
            });
          }
          return {
            code: 0,
            mes: '成功添加' + fname + '为好友'
          }
        }
      } catch (e) {
        return {
          code: 1,
          mes: '添加' + fname + '失败'
        }
      }
      break;
  }
}